/**
Copyright 2011 Michael Shick

This file is part of Joggle.

Joggle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Joggle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Joggle.  If not, see <http://www.gnu.org/licenses/>.
*/
package edu.gwu.cs.joggle;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.URI;

/**
 * Transmits data securely
 * @author Michael Shick <mfshick@gwmail.gwu.edu>
 * @version 0.1
 * @since alpha
 */
public class Connection
{
    /** Indicators of session status */
    public static final int ACTIVE = 0, INACTIVE = 1, CLOSED = 2, REJECTED = 3,
           INAUTHENTIC = 4, FAILED = 5, TIMED_OUT = 6;
    public static final int TIMEOUT = 5000, BUFFER_SIZE = 4 * 1024;

    private DataInputStream inbound;
    private DataOutputStream outbound;
    private Socket socket;
    private InetAddress host;
    private InetSocketAddress address;
    private int port, timeout;
    private byte[] buffer;

    /**
     * @param host InetAddress
     * @param port int
     */

    public Connection(InetAddress host, int port)
    {
        this(host, port, TIMEOUT);
    }

    /**
     * @param host InetAddress
     * @param port int
     * @param timeout int
     */
    public Connection(InetAddress host, int port, int timeout)
    {
        socket = new Socket();
        this.host = host;
        this.port = port;
        this.timeout = timeout;
        this.buffer = new byte[BUFFER_SIZE];
        address = new InetSocketAddress(host,port);
        try
        {
            socket.setSoTimeout(this.timeout);
        }
        catch(java.net.SocketException e)
        {
        }
    }

    /**
     * @returns int
     */
    public int connect()
    {
        try
        {
            socket.connect(address, timeout);
        }
        catch(java.net.SocketTimeoutException e)
        {
            return TIMED_OUT;
        }
        catch(java.io.IOException e)
        {
            return FAILED;
        }

        try
        {
            inbound = new DataInputStream(socket.getInputStream());
            outbound = new DataOutputStream(socket.getOutputStream());
        }
        catch(java.io.IOException e)
        {
            inbound = null;
            outbound = null;
            try
            {
                socket.close();
            }
            catch(Throwable f)
            {
            }
            return FAILED;
        }
        return ACTIVE;
    }

    /**
     * @returns int
     */
    public int disconnect()
    {
        try
        {
            socket.close();
            inbound = null;
            outbound = null;
            return INACTIVE;
        }
        catch(java.io.IOException e)
        {
            return FAILED;
        }
    }

    /**
     * @param p Packet
     * @returns int
     */
    public int send(Packet p)
    {
        byte data[] = p.getBytes();
        try
        {
            outbound.write(data, 0, data.length);
        }
        catch(java.io.IOException e)
        {
            return FAILED;
        }
        return FAILED; //TODO: fix
    }

    /**
     * @returns Packet
     */
    public Packet receive()
    {
        return null; //TODO: packetstuffs
    }
}
